<template>
  <j-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">

        <!-- <a-form-item label="物料料号" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['materielCode']" placeholder="请输入物料料号"></a-input>
        </a-form-item> -->
        <
        <a-form-item label="物料料号" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-popup
            v-decorator="['materielCode']"
            :trigger-change="true"
            org-fields="id,materiel_code,product_name,gauge,unit"
            dest-fields="materielId,materielCode,materielName,materielGague,unit"
            code="materiel_search"
            @callback="popupCallback"/>
        </a-form-item>
        <a-form-item label="物料名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['materielName']" placeholder="请输入物料名称"></a-input>
        </a-form-item>
        <a-form-item label="物料规格" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['materielGague']" placeholder="请输入物料规格"></a-input>
        </a-form-item>
        <!-- <a-form-item label="工厂编号" :labelCol="labelCol" :wrapperCol="wrapperCol">
           <a-input v-decorator="['factoryCode']" placeholder="请输入工厂编号"></a-input>
         </a-form-item> -->
        <a-form-item label="工厂编号" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-popup
            v-decorator="['factoryCode']"
            :trigger-change="true"
            org-fields="id,fac_code,fac_nameone,store_code,store_name"
            dest-fields="factoryId,factoryCode,factoryName,storeCode,storeName"
            code="factorysite_search"
            @callback="popupCallback"/>
        </a-form-item>
        <a-form-item label="工厂名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['factoryName']" placeholder="请输入工厂名称"></a-input>
        </a-form-item>
        <a-form-item label="仓库编号" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['storeCode']" placeholder="请输入仓库编号"></a-input>
        </a-form-item>
        <a-form-item label="仓库名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['storeName']" placeholder="请输入仓库名称"></a-input>
        </a-form-item>
        <a-form-item label="库存数量" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['stockNum']" placeholder="请输入库存数量"></a-input>
        </a-form-item>
        <a-form-item label="区域编码" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['areaCode']" placeholder="请输入区域编码"></a-input>
        </a-form-item>
        <a-form-item label="位置编码" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['locationCode']" placeholder="请输入位置编码"></a-input>
        </a-form-item>
        <a-form-item label="库存临界点" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['minimum']" placeholder="请输入库存临界点"></a-input>
        </a-form-item>
        <a-form-item label="单位" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['unit']" placeholder="请输入单位"></a-input>
        </a-form-item>
        <a-form-item label="期初库存" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['query3']" placeholder="请输入期初库存"></a-input>
        </a-form-item>
        <a-form-item label="期初日期" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <!--<a-input v-decorator="['unit']" placeholder="请输入期初日期"></a-input>-->
          <j-date placeholder="请选择期初日期" class="query-group-cust" v-decorator="['query4']"></j-date>
        </a-form-item>
        <a-form-item label="类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['query5']" placeholder="请输入类型"></a-input>
        </a-form-item>
        <a-form-item v-show="false" label="物料ID" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-show="false" v-decorator="['materielId']" placeholder="请输入物料ID"></a-input>
        </a-form-item>
        <a-form-item v-show="false" label="工厂ID" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-show="false" v-decorator="['factoryId']" placeholder="请输入工厂ID"></a-input>
        </a-form-item>

      </a-form>
    </a-spin>
  </j-modal>
</template>

<script>

  import {httpAction} from '@/api/manage'
  import pick from 'lodash.pick'
  import {validateDuplicateValue} from '@/utils/util'
  import JDate from '@/components/jeecg/JDate.vue'


  export default {
    name: "MesStockManageModal",
    components: {JDate},
    data() {
      return {
        form: this.$form.createForm(this),
        title: "操作",
        width: 800,
        visible: false,
        model: {},
        labelCol: {
          xs: {span: 24},
          sm: {span: 5},
        },
        wrapperCol: {
          xs: {span: 24},
          sm: {span: 16},
        },
        confirmLoading: false,
        validatorRules: {},
        url: {
          add: "/storage/mesStockManage/add",
          edit: "/storage/mesStockManage/edit",
        }
      }
    },
    created() {
    },
    methods: {
      add() {
        this.edit({});
      },
      edit(record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model, 'materielId', 'materielCode', 'materielName', 'materielGague', 'factoryId', 'factoryCode', 'factoryName', 'storeCode', 'storeName', 'stockNum', 'areaCode', 'locationCode', 'minimum', 'unit', 'query3', 'query4', 'query5'))
        })
      },
      close() {
        this.$emit('close');
        this.visible = false;
      },
      handleOk() {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if (!this.model.id) {
              httpurl += this.url.add;
              method = 'post';
            } else {
              httpurl += this.url.edit;
              method = 'put';
            }
            let formData = Object.assign(this.model, values);
            console.log("表单提交数据", formData)
            httpAction(httpurl, formData, method).then((res) => {
              if (res.success) {
                that.$message.success(res.message);
                that.$emit('ok');
              } else {
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })
          }

        })
      },
      handleCancel() {
        this.close()
      },
      popupCallback(row) {
        this.form.setFieldsValue(pick(row, 'materielId', 'materielCode', 'materielName', 'materielGague', 'factoryId', 'factoryCode', 'factoryName', 'storeCode', 'storeName', 'stockNum', 'areaCode', 'locationCode', 'minimum', 'unit', 'query3', 'query4', 'query5'))
      },


    }
  }
</script>